﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfAppKinectAR
{
    using Microsoft.Research.Kinect;
    using Microsoft.Research.Kinect.Nui;

    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Kinect Runtime
        /// </summary>
        private Runtime runtime;

        public MainWindow()
        {
            InitializeComponent();

            // Initialize Kinect Runtime
            runtime = new Runtime();
            runtime.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking);

            // Open Video Frame Stream & Regist Video Frame Handler
            runtime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
            runtime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(runtime_VideoFrameReady);

            // Regist Skelton Frame Handler
            runtime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(runtime_SkeletonFrameReady);
        }

        void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            int count = 0;
            tbStatus0.Text = "";
            // Example of Body Center Point
            var frame = e.SkeletonFrame;
            foreach (var data in frame.Skeletons)
            {
                if (data.TrackingState == SkeletonTrackingState.Tracked)
                {
                    if (!String.IsNullOrEmpty(tbStatus0.Text))
                    {
                        tbStatus0.Text += ",";
                    }
                    tbStatus0.Text += String.Format("{0}:{1}", data.TrackingID, data.TrackingState.ToString());
                    Vector bodyCenter = data.Joints[JointID.Spine].Position;
                    if (count == 0)
                    {
                        tbStatus1.Text = String.Format("{0}:({1:0.00},{2:0.00},{3:0.00},{4:0.00})",data.TrackingID, bodyCenter.X, bodyCenter.Y, bodyCenter.Z, bodyCenter.W);
                    }
                    else
                    {
                        tbStatus2.Text = String.Format("{0}:({1:0.00},{2:0.00},{3:0.00},{4:0.00})", data.TrackingID, bodyCenter.X, bodyCenter.Y, bodyCenter.Z, bodyCenter.W);
                    }
                    count++;
                }
                if (count > 2)
                {
                    break;
                }
            }
        }

        void runtime_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
            PlanarImage image = e.ImageFrame.Image;
            VideoFrameImage.Source = BitmapImage.Create(image.Width, image.Height
                , 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel);
        }
    }
}
